define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Transfer, GoPage, GoAddress, GoDialog,$timeout) {

		//判断登录的身份类型--无须分类
		//$scope.user_type=Auth.check_user_type();//1:制造商，2:品牌代理商，3:代理商/分仓	4:经销商

		//加载数据
		$scope.input = new Object();
		$scope.input.price="";
		$scope.input.region_id = "";
		$scope.input.keywords="";
		$scope.order = 0;
		$scope.data_init = function() {
			Transfer.getlist($scope.input.price,$scope.input.region_id, $scope.input.keywords, $scope.order, 1, 10, function(data) {
				$scope.list = data.info;
				$scope.price=data.price;
				$scope.total = data.page.count;//符合条件的数据共有多少条
				$scope.input.nPage = 1;
			})
		}
		$scope.data_init();

		/***省市区三级联动***/
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function (data) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.district_items = null;
		};
		$scope.listen_city = function (data) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};
		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		//搜索
		$scope.search = function() {
			$scope.input.region_id = "";
			if($scope.input.district != null){
				$scope.input.region_id = $scope.input.district.id; //地区id
			}else if($scope.input.city != null){
				$scope.input.region_id =  $scope.input.city.id; //城市id
			}else if($scope.input.province != null){
				$scope.input.region_id = $scope.input.province.id; //省份id
			}else{
			    $scope.input.region_id="";
			}
			$scope.data_init();
		}


		//排序--发布时间--不要
//		$scope.orderByTime = function() {
//			$scope.input.nPage = 1;
//			if($scope.order == 1) {
//				$scope.order = 2;
//			} else {
//				$scope.order = 1;
//			}
//			Transfer.getlist($scope.input.region_id, $scope.input.keywords, $scope.order, 1, 10, function(data) {
//				$scope.list = data.info;
//			})
//		};

		//默认排序

		$scope.orderMo=function(){
		    $scope.input.nPage = 1;
		    $scope.order=0;
		    Transfer.getlist($scope.input.price,$scope.input.region_id, $scope.input.keywords, $scope.order, 1, 10, function(data) {
                $scope.list = data.info;
            })
		}
		//排序--价格
		$scope.orderByPrice = function() {
			$scope.input.nPage = 1;
			if($scope.order == 3) {
				$scope.order = 4;
			} else {
				$scope.order = 3;
			}
			Transfer.getlist($scope.input.price,$scope.input.region_id, $scope.input.keywords, $scope.order, 1, 10, function(data) {
				$scope.list = data.info;
			})
		};

		$scope.onshow_detail = function(good_id) {
			GoPage.go_states("transferdetail", { "good_id": good_id });
		}

		//价格选择
		$scope.get_price=function(value){
		    $scope.input.price=value;
		    $scope.input.nPage = 1;
		    $scope.data_init();
		}

		//下拉刷新
		$scope.isLoad = false; //可以加载
		$scope.input.nPage = 1;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Transfer.getlist($scope.input.price,$scope.input.region_id, $scope.input.keywords, $scope.order, 1, 10,function(data) {
				if(data!=null){
					$scope.list = data.info;
				}else{
					$scope.list = new Object();
				}
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Transfer.getlist($scope.input.price,$scope.input.region_id, $scope.input.keywords, $scope.order,$scope.input.nPage+1,10,function(data) {
				if(data!=null){
					if($scope.list!=undefined){
						$scope.list = $scope.list.concat(data.info);
						$scope.input.nPage += 1;
					}
					else{
						$scope.list = data.info;
					}
				}
			},
			function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			});
		};


	}

	controller.$inject = ['$scope', 'Transfer', 'GoPage', 'GoAddress', 'GoDialog','$timeout'];

	return app.registerController("transferctrl",controller);
});